﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.ApplicationCache;
using MicropleSoft.WmsApp.Library.ApplicationObserver;
using MicropleSoft.WmsApp.Library.LookUpInterface;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.Controls
{
    public partial class XLookUpEdit : DevExpress.XtraEditors.LookUpEdit, IDataBinder, IDataSource, ILookUpViewHeaderBuilder
    {
        public XLookUpEdit()
        {
            InitializeComponent();
        }

        protected override void OnLoaded()
        {
            base.OnLoaded();
            if (GlobalConfig.RunTimeMode)
            {
                this.Properties.NullText = "";
                var form = this.FindForm();
                if (form != null)
                {
                    form.Closed += (o, e) =>
                    {
                        if (DataSourceUseCache)
                        {
                            DataSourceControlObserverFactory.CreateDataSourceControlObserver()
                                .RemoveControl(this, DataSourceKey);
                        }
                    };
                }
            }
        }

        public void ExcuteDataBinding(WmsApp.Library.IBaseDomainObject baseDomainObject)
        {
            if (String.IsNullOrEmpty(PropertyName))
            {
                throw new PropertyNameNotInitializedException();
            }

            if (baseDomainObject == null)
            {
                throw new ArgumentNullException("baseDomainObject");
            }

            var propertyInfo = baseDomainObject.GetType().GetProperty(PropertyName);
            if (propertyInfo == null)
            {
                //throw new PropertyNotExistException();
                return;
            }

            this.DataBindings.Clear();
            this.DataBindings.Add("EditValue", baseDomainObject, PropertyName);

        }

        public string PropertyName { get; set; }

        [DefaultValue(false)]
        public bool IsPrimaryKey { get; set; }


        public bool ReadOnly
        {
            get
            {
                return this.Properties.ReadOnly;
            }
            set
            {
                this.Properties.ReadOnly = value;
            }
        }

        

        public void BuildColumns(List<LookUpViewHeader> lookUpViewHeaders)
        {
            this.Properties.Columns.Clear();

            foreach (var lookUpViewHeader in lookUpViewHeaders)
            {
                var lookUpColumnInfo = new LookUpColumnInfo(lookUpViewHeader.Field, lookUpViewHeader.Caption, lookUpViewHeader.Width);
                lookUpColumnInfo.Visible = lookUpViewHeader.IsVisual;
                this.Properties.Columns.Add(lookUpColumnInfo);
                if (lookUpViewHeader.IsEditValue)
                {
                    this.Properties.ValueMember = lookUpViewHeader.Field;
                }
                if (lookUpViewHeader.IsDisplayValue)
                {
                    this.Properties.DisplayMember = lookUpViewHeader.Field;
                }
            }

            int width = lookUpViewHeaders.Where(lookUpViewHeader => lookUpViewHeader.IsVisual)
                .Sum(lookUpViewHeader => lookUpViewHeader.Width);
            this.Properties.PopupFormWidth = width + 2;

            this.Properties.NullText = "";
            if (DataSourceUseCache)
            {
               this.Properties.DataSource = ((BaseDataWrap)GetBaseDataProvider().GetBaseDataByKey(DataSourceKey)).BaseDataSource;
            }
            else
            {
                this.Properties.DataSource = ((BaseDataWrap)GetBaseDataProvider().GetRealTimeBaseDataByKey(DataSourceKey)).BaseDataSource;
            }

        }

        public IBaseDataProvider GetBaseDataProvider()
        {
            return CacheManage.GetBaseDataProvider();
        }

        [DefaultValue(true)]
        public bool DataSourceUseCache { get; set; }

        public string DataSourceKey { get; set; }

        public void SetDataSource(DataTable data)
        {
            this.Properties.DataSource = data;
        }
    }
}
